package com.ktg.mes.pda.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.ktg.mes.pda.mapper.PdaMenuPersonMapper;
import com.ktg.mes.pda.domain.PdaMenuPerson;
import com.ktg.mes.pda.service.IPdaMenuPersonService;



/**
 * PDA菜单关联用户Service业务层处理
 *
 * @author byteco
 * @date 2022-12-31
 */
@Service
public class PdaMenuPersonServiceImpl extends ServiceImpl<PdaMenuPersonMapper, PdaMenuPerson> implements IPdaMenuPersonService
{




    /**
     * 查询PDA菜单关联用户
     *
     * @param entity PDA菜单关联用户
     * @param page 分页对象
     * @return PDA菜单关联用户
     */
    @Override
    public IPage<PdaMenuPerson> queryPageList(PdaMenuPerson entity, Page<PdaMenuPerson> page) {
        QueryWrapper<PdaMenuPerson> lqw = new QueryWrapper<PdaMenuPerson>(entity);
        lqw.orderByDesc("create_time");
        IPage<PdaMenuPerson> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询PDA菜单关联用户
     *
     * @param entity PDA菜单关联用户
     * @return PDA菜单关联用户
     */
    @Override
    public List<PdaMenuPerson> queryList(PdaMenuPerson entity) {
        QueryWrapper<PdaMenuPerson> lqw = new QueryWrapper<PdaMenuPerson>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public PdaMenuPerson getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增PDA菜单关联用户
     *
     * @param entity PDA菜单关联用户
     * @return 结果
     */
    @Override
    public boolean insert(PdaMenuPerson entity)
    {
        List<PdaMenuPerson> list= queryList(entity);
        if (list.size()==0){
            entity.setCreateBy(SecurityUtils.getUsername());
            entity.setCreateTime(DateUtils.getNowDate());
          return   this.save(entity);
        }
            return false;
    }

    /**
     * 修改PDA菜单关联用户
     *
     * @param entity PDA菜单关联用户
     * @return 结果
     */
    @Override
    public boolean update(PdaMenuPerson entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除PDA菜单关联用户
     *
     * @param recordIds 需要删除的PDA菜单关联用户主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除PDA菜单关联用户信息
     *
     * @param recordId PDA菜单关联用户主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<PdaMenuPerson> buildQueryWrapper(PdaMenuPerson entity) {
        LambdaQueryWrapper<PdaMenuPerson> lqw = Wrappers.lambdaQuery();
                    lqw.eq(entity.getPdaMenuId() != null, PdaMenuPerson::getPdaMenuId, entity.getPdaMenuId());
        return lqw;
    }
}
